package com.brillilab.service.core.message.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.dao.mapper.message.MessageMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.lab.Lab;
import com.brillilab.domain.po.message.Message;
import com.brillilab.domain.vo.message.MessageDetailVo;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.message.IMessageService;
import com.brillilab.service.module.message.template.enums.MessageTypeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
	private ILabService labService;

    @Override
    public Message selectById(Long messageId) {
        return messageMapper.selectById(messageId);
    }

    @Override
    public boolean update(Message message) {
        message.setUpdateTime(new Date());
        return messageMapper.updateById(message) > 0;
    }

    @Override
    public boolean insert(Message message) {
        return messageMapper.insert(message) > 0;
    }

	@Override
	public List<Message> getTopUnReadMsgList(Integer top,  Long userId, List<Integer> msgTypeList) {
		return messageMapper.getTopUnReadMsgList(top, userId,msgTypeList);
	}

	@Override
	public PageVo<Message> getPageUnReadMsgList(Long userId, List<Integer> msgTypeList, int pageNum, int pageSize) {
		IPage<Message> page = new Page<>(pageNum, pageSize);
		LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Message::getReceiverId, userId);
		queryWrapper.eq(Message::getIsRead, 0);
		queryWrapper.in(Message::getType,msgTypeList);
		queryWrapper.orderByDesc(Message::getCreateTime);
		IPage<Message> iPage = messageMapper.selectPage(page, queryWrapper);
		return new PageVo<Message>(iPage);
	}

	@Override
	public PageVo<Message> getMessageList( Long userId, Integer isRead, int pageNum, int pageSize) {
		IPage<Message> page = new Page<>(pageNum, pageSize);
		LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Message::getReceiverId, userId);
		if (isRead != null) {
			queryWrapper.eq(Message::getIsRead, isRead);
		}
		queryWrapper.orderByDesc(Message::getCreateTime);
		IPage<Message> iPage = messageMapper.selectPage(page, queryWrapper);
		return new PageVo<Message>(iPage);
	}

	@Override
	public boolean allRead(Long userId) {
		int allRead = messageMapper.allRead(userId);
		return allRead>0;
	}

	@Override
	public void reviewRead(MessageTypeEnum typeEnum, Long referId) {
		LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Message::getType, typeEnum.getValue());
		queryWrapper.eq(Message::getReferId, referId);
		queryWrapper.eq(Message::getIsRead, BoolEnum.FALSE.getValue());
		Message message = new Message();
		message.setIsRead(BoolEnum.TRUE.getValue());
		messageMapper.update(message, queryWrapper);
	}
	@Override
	public Integer getUnReadMsgCount(Long userId) {
    	return messageMapper.getUnReadMsgCount(userId);
	}

    @Override
    public List<Message> insertBatch(List<Message> messages) {
    	if(messages!=null && messages.size()>0){
    		messageMapper.insertBatch(messages);
    		return messages;
		}
        return null;
    }

	@Override
	public Message getMessage(Long messageId,Long userId) {
		//普通
		Message message = this.selectById(messageId);
		Assert.notNull(message,"消息不存在!");
		Assert.isTrue(this.canRead(message,userId), "参数错误");
		//群发
		if(!message.getReceiverId().equals(userId) && StringUtils.isNotBlank(message.getReceiverIds())){
			message = this.selectOne(message.getReferId(),userId);
			Assert.notNull(message,"消息不存在!");
		}
		return message;
	}

    @Override
    public MessageDetailVo getMessageDetail(Long messageId,Long labId,Long userId) {
		Assert.isTrue(messageId!=null && labId!=null && userId!=null,"参数缺失！");
		Message message=this.getMessage(messageId,userId);
		MessageDetailVo detailVo=MyBeanUtils.copyBean(message,MessageDetailVo.class);
		Lab lab=labService.selectById(message.getLabId());
		Assert.notNull(lab,"实验室不存在！");
		JSONObject data=JSONObject.parseObject(message.getData());
		detailVo.setContent(data.getString("content"));
		detailVo.setLabName(lab.getName());
		detailVo.setLabImage(lab.getImageUrl());
		return detailVo;
    }

    @Override
    public boolean canRead(Message message,Long userId){
        return message.getReceiverId().equals(userId) || Arrays.asList(message.getReceiverIds().split(",")).contains(userId.toString());
    }

	@Override
	public Message selectOne(Long referId,Long receiverId) {
		return messageMapper.selectOne(
				new LambdaQueryWrapper<Message>()
						.eq(Message::getReferId,referId)
						.eq(Message::getReceiverId,receiverId));
	}
}
